package renshi.action;

import java.sql.Connection;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import renshi.base.BaseAction;
import renshi.dao.StudentDAO;
import renshi.model.Apply;
import renshi.utils.DBUtils;

public class StudentAction extends BaseAction{

	private static final long serialVersionUID = 1L;
	private String loginname; 
	private String begintime;
	private String endtime;
	private int days;
	public int getDays() {
		return days;
	}

	public void setDays(int days) {
		this.days = days;
	}

	public String getBegintime() {
		return begintime;
	}

	public void setBegintime(String begintime) {
		this.begintime = begintime;
	}

	public String getEndtime() {
		return endtime;
	}

	public void setEndtime(String endtime) {
		this.endtime = endtime;
	}

	private String reason;
	private String remark;
	private int aid;
	public int getAid() {
		return aid;
	}

	public void setAid(int aid) {
		this.aid = aid;
	}

	private int status;
	
	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	HttpServletRequest request = ServletActionContext.getRequest();
	HttpSession session = request.getSession();
	public String getReason() {
		return reason;
	}

	public void setReason(String reason) {
		this.reason = reason;
	}

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	private String pwd;
	private String newpwd;
	public String getNewpwd() {
		return newpwd;
	}

	public void setNewpwd(String newpwd) {
		this.newpwd = newpwd;
	}

	public String getLoginname() {
		return loginname;
	}

	public void setLoginname(String loginname) {
		this.loginname = loginname;
	}

	public String getPwd() {
		return pwd;
	}

	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

	StudentDAO studentDAO;
	//-----------------------------�ж�ѧ������״̬
   public String execute(HttpServletRequest req, HttpServletResponse resp)throws Exception{
		
		Connection con = DBUtils.getConnection();
        con.setAutoCommit(false);
        loginname=(String)session.getAttribute("loginname");
		studentDAO = new StudentDAO(con);
		Apply apply = studentDAO.status(loginname);
        con.commit();
        if(apply!=null){
        	System.out.print("111");
        	req.setAttribute("apply", apply);
        	return "statusSuccess";
        	
        }
    	else
        	return "loginFail";
	}
	//-------------------------------ѧ���޸�����
	
   
	public String updatepwd()throws Exception{
		Connection con = DBUtils.getConnection();
        con.setAutoCommit(false);
		
		studentDAO = new StudentDAO(con);
		boolean flag = studentDAO.updatepwd(loginname, newpwd);
        con.commit();
        if(flag==true){
        	return "updatepwdSuccess";
        }
    	else
        	return "updatepwdFail";
		
        
        
	}
	//-------------------��д��ٵ�
	public String apply()throws Exception{
		
		Connection con = DBUtils.getConnection();
        con.setAutoCommit(false);
        loginname=(String)session.getAttribute("loginname");
		studentDAO = new StudentDAO(con);
		boolean flag = studentDAO.apply(loginname,begintime,endtime,reason,remark,status);
        con.commit();
        if(flag==true){
        	return "applySuccess";
        }
    	else
        	return "loginFail";
	}
//-----------------��ȡȫ������ٵ�
public String queryApplylist()throws Exception{
		
		Connection con = DBUtils.getConnection();
        con.setAutoCommit(false);
        loginname=(String)session.getAttribute("loginname");
		studentDAO = new StudentDAO(con);
		List<Apply> applylist =studentDAO.queryApplylist(loginname);
        con.commit();
        if(applylist!=null){
        	request.setAttribute("applylist", applylist);
        	request.getAttribute("applylist");
        	System.out.println("StudentAction�е�applylist��==null");
        	return "sapplylist";
        	
        }
    	else
        	return "noHistory";
	}

//------------------��ȡ��ٵ�����ϸ��Ϣ
public String applydetail()throws Exception{
	
	Connection con = DBUtils.getConnection();
    con.setAutoCommit(false);
    String applyid = request.getParameter("applyid");
    int numid=0;
    if(applyid!=null){
       numid=Integer.parseInt(applyid);
    }
	studentDAO = new StudentDAO(con);
	Apply apply = studentDAO.applydetail(numid);
    con.commit();
    if(apply!=null){
    	request.setAttribute("apply",apply);
    	request.getAttribute("apply");
    	System.out.println("StudentAction�е�apply��==null");
    	return "sapplydetailh";
    	
    }
	else
    	return "loginFail";
}
//------------��ȡ���޸ĵ���ٵ���Ϣ
public String updateApplylist()throws Exception{
	
	Connection con = DBUtils.getConnection();
    con.setAutoCommit(false);
    loginname=(String)session.getAttribute("loginname");
    studentDAO = new StudentDAO(con);
	Apply apply = studentDAO.updateApplylist(loginname);
    con.commit();
    if(apply!=null){
    	request.setAttribute("apply",apply);
    	request.getAttribute("apply");
    	System.out.println("StudentAction�е�apply��==null");
    	return "sapplydetail";
    	
    }
	else
    	return "noapply";
}
//--------------�ж����״̬
public String status()throws Exception{
	
	Connection con = DBUtils.getConnection();
    con.setAutoCommit(false);
    loginname=(String)session.getAttribute("loginname");
	studentDAO = new StudentDAO(con);
	Apply apply = studentDAO.status(loginname);
    con.commit();
    if(apply!=null){
        request.setAttribute("apply", apply);
        Apply applystatus = studentDAO.applystatus(loginname);
        request.setAttribute("applystatus", applystatus);
    	return "statusSuccess";
    	
    }
	else
    	return "statusEmpty";
}
//----------------�޸���ٵ������״̬�޸ģ�û����˵���ٵ��ſ����޸�
public String updateApply()throws Exception{
	Connection con = DBUtils.getConnection();
    con.setAutoCommit(false);
	
	studentDAO = new StudentDAO(con);
	boolean flag = studentDAO.updateApply(status, reason, remark);
    con.commit();
    if(flag==true){
    	return "updatepwdSuccess";
    }
	else
    	return "updatepwdFail";
}
//-----------��ȡ����ٵ���ٵ���Ϣ
public String cancle()throws Exception{
	
	Connection con = DBUtils.getConnection();
    con.setAutoCommit(false);
    loginname=(String)session.getAttribute("loginname");
    studentDAO = new StudentDAO(con);
	Apply apply = studentDAO.cancle(loginname);
    con.commit();
    if(apply!=null){
    	request.setAttribute("apply",apply);
    	request.getAttribute("apply");
    	System.out.println("StudentAction�е�apply��==null");
    	return "cancleleave";
    	
    }
	else
    	return "nocancleleave";
}
//-------------�ж��Ƿ�����������
public String applyLeave()throws Exception{
	
	Connection con = DBUtils.getConnection();
    con.setAutoCommit(false);
    loginname=(String)session.getAttribute("loginname");
    studentDAO = new StudentDAO(con);
	Apply apply = studentDAO.applyLeave(loginname);
    con.commit();
    if(apply!=null){
    	request.setAttribute("apply",apply);
    	request.getAttribute("apply");
    	System.out.println("���������");
    	
    	return "notallowApply";
    	
    }
	else
    	return "allowApply";
}
//----------------------���
public String cancleApply()throws Exception{
	
	Connection con = DBUtils.getConnection();
    con.setAutoCommit(false);
    String applyid = request.getParameter("aid");
    System.out.println(applyid);
    int numid=0;
    if(applyid!=null){
       numid=Integer.parseInt(applyid);
    }
	studentDAO = new StudentDAO(con);
	boolean flag = studentDAO.cancleApply(numid);
    con.commit();
    if(flag==true){
    	return "cancleSuccess";
    }
	else
    	return "updatepwdFail";
}
}


